//
// C++ Interface: activityplot
//
// Description:
//
//
// Author: Willi Ballenthin <wilbal1087@gmail.com>, (C) 2008
//
// Copyright: See COPYING file that comes with this distribution
//
//
#ifndef ACTIVITYPLOT_H
#define ACTIVITYPLOT_H

#include "activity.h"
#include "activitydbinterface.h"
#include <QtCore>
#include <QGraphicsItem>
#include <QtGui>

class ActivityPlotMap;

/**
    @author Willi Ballenthin <wilbal1087@gmail.com>
*/
class ActivityPlot : public QObject, public QGraphicsItem
{
        Q_OBJECT
    public:
        ActivityPlot ( ActivityDbInterface*,
                       QString property, 
                       QGraphicsItem *parent = 0 );

        ActivityPlot ( ActivityDbInterface*,
                       QGraphicsItem *parent = 0 );


        QRectF boundingRect() const;
        QString getProperty();

        void paint (QPainter *painter,
                    const QStyleOptionGraphicsItem *option,
                    QWidget *widget );

        enum plotEdges{PLOTTOP = 30,
                       PLOTBOTTOM = 230,
                       PLOTLEFT = 0,
                       PLOTRIGHT = 650,
                       TOTALHEIGHT = 290,
                       TOTALWIDTH = 690
                  };

    signals:
        void focused ( int id );

    public slots:
        void updateModel();
        void setProperty(QString);

    protected:
        void mousePressEvent ( QGraphicsSceneMouseEvent * event );
        void hoverMoveEvent ( QGraphicsSceneHoverEvent * event );

    private:
        ActivityDbInterface* interface;
        QVector<Activity> vector;
        QVector<ActivityPlotMap> mapper;
        QDate dateStart;
        QDate dateEnd;
        QString property;
        int days;
        float max;
        float min;
        float sum;
        float avg;
        float xInterval;
        float lastHoverX;

        void subpathGroup ( QPainterPath* path,
                            QVector<ActivityPlotMap> vector );

        ActivityPlotMap* binSearch ( float value,
                                     ActivityPlotMap* data,
                                     int len );


        void paintBg(QPainter* painter);
        void drawDate(QPainter* painter, float x,
                      float y, QDate date);
        void drawValue(QPainter* painter, float x,
                       float y, float value);
        float getProperty(Activity activity);


};

/**
*   A class that converts between the Activity vector
*   and points on the ActivityPlot.
*/
class ActivityPlotMap
{
    public:
        /// Id value of the activity.
        int index;
        /// The date of the activity.
        QDate date;
        /// Day since the start of the plot.
        int day;
        /// Property value, ie. distance, effort.
        float propValue;
        /// The x coordinate of this activity in the plot.
        float xValue;
        /// The y coordinate of this activity in the plot.
        float yValue;

        /**
        * Given bounding data, determines the yValue
        * of a distance.
        * @param y The height of the plot.
        * @param max The maximum value in the plot domain.
        */
        void setY ( int y, float max )
        {
            //HACK: this is not general and will not work
            // with resizing
            float yInterval = ( y - 30 ) / max;
            yValue = y - propValue * yInterval;
        }

        void setX(QDate firstDay, float xInterval)
        {
            day = firstDay.daysTo(date);
            xValue = day * xInterval;
        }
};

#endif
